package cn.edu.pku.ss.music.activities;

import java.util.concurrent.ExecutionException;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import cn.edu.pku.ss.music.R;
import cn.edu.pku.ss.music.net.RandomCodeTask;
import cn.edu.pku.ss.music.util.ConnectionURLs;
import cn.edu.pku.ss.music.util.Util;

public class InputPhoneNumberActivity extends Activity {
	private TextView title_textview;
	private TextView dialog_phone_number_textview;
	private TextView registration_phone_textview;
	private Button send_pin_button;
	private Button confirm_button;
	private Button cancel_button;
	private Dialog dialog;
	
	private String url = ConnectionURLs.RANDOM_CODE;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.input_phone_layout);
		View layout = findViewById(R.id.input_phone_header);
		title_textview = (TextView) layout.findViewById(R.id.header_title);
		dialog_phone_number_textview = (TextView) findViewById(R.id.registration_dialog_message);
		registration_phone_textview = (TextView) findViewById(R.id.registration_phone);
		send_pin_button = (Button) findViewById(R.id.send_pin_button);
		confirm_button = (Button) findViewById(R.id.registration_dialog_ok);
//		cancel_button = (Button) findViewById(R.id.registration_dialog_cancel);
		
		title_textview.setText(R.string.input_phone_number_title);
		send_pin_button.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				String phone_number = registration_phone_textview.getText().toString();
				if(Util.isEmpty(phone_number)){
					Toast.makeText(InputPhoneNumberActivity.this, R.string.empty_phone_number, 
							Toast.LENGTH_SHORT).show();
					return;
				}
				
				dialog = new Dialog(InputPhoneNumberActivity.this, R.style.dialog);
				dialog.setContentView(R.layout.register_dialog_layout);
				dialog.setCanceledOnTouchOutside(true);
				dialog_phone_number_textview.setText(phone_number);
				dialog.show();
			}
		});
		
		confirm_button.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				RandomCodeTask code_task = new RandomCodeTask();
				code_task.execute(url);
				
				String code = null;
				try {
					code = code_task.get();
				} catch (InterruptedException e) {
					e.printStackTrace();
				} catch (ExecutionException e) {
					e.printStackTrace();
				}
				
				System.out.println("random code = " + code);
			}
		});
		
		cancel_button.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				dialog.dismiss();
			}
		});
	}
}
